package com.snowher.play.controller;

import com.snowher.play.entity.BaseResult;
import com.snowher.play.entity.LocationEntity;
import com.snowher.play.service.ISaveLocationService;
import com.snowher.play.utils.TextUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RequestMapping(value = "/location")
@Controller
public class LocationController {

    @Resource
    ISaveLocationService mSaveLocationService;

    public LocationController() {
    }

    /**
     * 记录当前位置坐标
     *
     * @param httpServletRequest HttpServlet
     * @return 当前数据结果
     * <p>
     * http://localhost:8080/location/recordLocation?deviceId=fdsauf8&userName=hejingzhou&lng=098&lat=3224.4&detail=%E9%95%BF%E5%9F%8Ehttp://localhost:8080/location/recordLocation?deviceId=fdsauf8&userName=hejingzhou&lng=098&lat=3224.4&detail=%E9%95%BF%E5%9F%8E
     */
    @ResponseBody
    @RequestMapping(value = "/recordLocation")
    public BaseResult<LocationEntity> recordLocation(HttpServletRequest httpServletRequest) {
        String deviceId = httpServletRequest.getParameter("deviceId");
        String username = httpServletRequest.getParameter("userName");
        String lng = httpServletRequest.getParameter("lng");
        String lat = httpServletRequest.getParameter("lat");
        String detail = httpServletRequest.getParameter("detail");

        BaseResult<LocationEntity> baseResult = new BaseResult();
        if (!TextUtils.isEmpty(deviceId) &&
                !TextUtils.isEmpty(username) &&
                !TextUtils.isEmpty(lng) &&
                !TextUtils.isEmpty(lat) &&
                !TextUtils.isEmpty(detail)) {
            LocationEntity locationEntity = new LocationEntity(deviceId, username, lng, lat, detail);
            baseResult.setData(locationEntity);
            baseResult.setSuccess(true);
            baseResult.setErrorCode(0);
            System.out.println(locationEntity.toString());
            int insertLine = mSaveLocationService.saveLocation(locationEntity);
            System.out.println(insertLine);
        }
        return new BaseResult(false, null, 0);
    }


}
